/**
 * Created by jihb on 5/3/16.
 */
Ext.define('JYnovel.controller.system.RolePermissionController',{
    extend:'Ext.app.Controller',
    views:[
        'system.RolePermissionView',
        'system.RoleForm'
    ],
    models:[
        'system.RoleModel'
    ],
    stores:[
        'system.RoleStore'
    ],
    refs:[{
        ref:'RoleForm',
        selector:'RoleForm'
    },{
        ref:'RoleGridPanel',
        selector:'RoleGridPanel'
    },{
        ref:'RoleGridPanelRightMenu',
        selector:'RoleGridPanelRightMenu'
    }],
    init:function () {
        this.control({
            'RoleGridPanel':{
                itemcontextmenu:this.roleGridPanelItemcontextmenu
            },
            'RoleGridPanel button[name=roleAddBtn]':{
                click:this.roleAddBtnClick
            },
            'RoleForm button[name=addRoleOkBtn]':{
                click:this.addRoleOkBtnClick
            },
            'RoleGridPanelRightMenu menuitem[name=roleEditMenuItem]':{
                click:this.roleEditMenuItemClick
            },
            'RoleGridPanelRightMenu menuitem[name=roleDeleteMenuItem]':{
                click:this.roleDeleteMenuItemClick
            }
        });
    },
    roleGridPanelItemcontextmenu:function (grid, record, item, index, e, eOpts ) {
        e.preventDefault();
        var menu = this.getRoleGridPanelRightMenu();
        if (!menu){
            menu = Ext.widget('RoleGridPanelRightMenu');
        }
        menu.showAt(e.getXY());
    },
    /**
     * 添加角色
     */
    roleAddBtnClick:function () {
        this.showRoleForm('添加角色');
    },

    /**
     * 编辑角色信息
     */
    roleEditMenuItemClick:function () {
        var grid = this.getRoleGridPanel();
        var selectRecord = ExtGetSelectRow(grid);
        this.showRoleForm('编辑角色',selectRecord.get('id'));
    },

    /**
     * 显示角色form
     * @param title
     * @param id
     */
    showRoleForm:function (title,id) {
        var form = Ext.widget('RoleForm');
        form.showFormWin(title,id);
    },
    /**
     * 确定提交表单
     */
    addRoleOkBtnClick:function () {
        var grid = this.getRoleGridPanel();
        var form = this.getRoleForm();
        var id = form.down('hiddenfield[name=id]').getValue();
        var url = "/rolepermission/addrole";
        if(id){
            url = "/rolepermission/updaterole";
        }
        ExtSubmitForm(form,url,function () {
            form.closeFormWin();
            grid.getStore().reload();
        });
    },
    /**
     * 删除角色
     */
    roleDeleteMenuItemClick:function () {
        var grid = this.getRoleGridPanel();
        var selectRecord = ExtGetSelectRow(grid);
        Ext.Msg.confirm('','是否确定删除"'+selectRecord.get('name')+'"?',function (btn) {
            if (btn=='yes'){
                //确定删除
                ExtAjaxSubmit({
                    url:getUrl('/rolepermission/deleterole'),
                    method:'POST',
                    params:{
                        id:selectRecord.get('id')
                    },
                    masktarget:grid
                },function (data) {
                    if(data.code==0){
                        grid.getStore().reload();
                    }
                });
            }
        });
    }
});